package com.testing.class5;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    public static void main(String[] args) {
        List<String> roylist=new ArrayList<>();

        //增
        //如果不指定下标，默认往最后添加。
        roylist.add("roy");
        roylist.add("will");
        roylist.add("will");
        roylist.add("肉");
        //指定下标
        roylist.add(1,"夜宵");
        //toString方法在AbstractCollection中实现
        System.out.println(roylist.toString());

        //删
        //删除值为will的元素
//        roylist.remove("will");
        //删除下标为1的元素
        roylist.remove(1);
        System.out.println(roylist);

        //改
        roylist.set(1,"烧烤");
        System.out.println(roylist);

        //查
        //1、通过下标进行遍历
        for(int i=0;i<roylist.size();i+=2){
            System.out.println(roylist.get(i)+"帅");
        }


        //2、foreach循环
        for (String s : roylist) {
            System.out.println("这是foreach循环"+s);
        }

        //3、Iterator循环
        Iterator<String> lit = roylist.iterator();
        while(lit.hasNext()){
            System.out.println("这是iterator循环"+lit.next());
        }



    }
}
